package vn.gq.cd.view;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import vn.gq.app4vn.doctruyen.ChapterActivity;
import vn.gq.app4vn.doctruyen.Configuration;
import vn.gq.app4vn.doctruyen.R;
import vn.gq.cd.model.CategoryAdapter;
import vn.gq.cd.model.bean.CategoryItem;
import vn.gq.cd.widget.BaseFragment;
import vn.gq.tym.utils.IClientListener;
import vn.gq.tym.utils.OnNetworkListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

public class CategoryFragment extends BaseFragment implements OnItemClickListener, OnNetworkListener, OnClickListener {

	public static final String PREV_SCR_TAG = "prev_scr_tag";
	
	private ListView mListView;
	private ProgressBar mProgressBar;
	private CategoryAdapter mAdapter;
	private String mDataPath;
	private View mBounder;
	private Button mBtnRetry;
	
	public static CategoryFragment newInstance(String dataPath) {
		CategoryFragment f = new CategoryFragment();
		Bundle args = new Bundle();
		args.putString("PATH", dataPath);
		f.setArguments(args);
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			mDataPath = getArguments().getString("PATH");
			if ("".equals(mDataPath)) mDataPath = "";
		}
		setRetainInstance(true);
		setHasOptionsMenu(true);
	}

	@Override
	public int getLayoutId() {
		return R.layout.fragment_chapter;
	}

	@Override
	public int getContainerId() {
		return R.id.layout_container;
	}
	
	@Override
	public void createView(Bundle savedInstanceState) {
		mListView = (ListView) findViewById(R.id.list);
		mListView.setOnItemClickListener(this);
		mBounder = findViewById(R.id.nonetwork);
		mBtnRetry = (Button) findViewById(R.id.btn_reconnect);
		mBtnRetry.setOnClickListener(this);
		mProgressBar = (ProgressBar) findViewById(R.id.loading);
		mAdapter = new CategoryAdapter(getActivity(), this);
		load(false);
	}

	public void load(final boolean reload) {
		mProgressBar.setVisibility(View.VISIBLE);
		mAdapter.load(mDataPath, new Runnable() {
			
			@Override
			public void run() {
				mListView.setAdapter(mAdapter);
				mProgressBar.setVisibility(View.GONE);
				if (getActivity() instanceof IClientListener && !reload) {
					((IClientListener) getActivity()).onDone(0, null);
				}
			}
		});
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (id > -1) {
			CategoryItem catItem = (CategoryItem) mAdapter.getItem(position);
//			ChapterFragment chapterFragment = ChapterFragment.newInstance(catItem);
//			startFragment(chapterFragment.getContainerId(), chapterFragment, chapterFragment.getFragmentTag(), false);
			ChapterActivity.startActivity(getActivity(), catItem);
			mAdapter.setSelectedPos(position);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mAdapter != null) {
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onNoNetwork() {
		mBounder.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		mBounder.setVisibility(View.GONE);
		load(true);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(Configuration.MULTI_STORIES_APP ?  R.menu.main : R.menu.none, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
//		case R.id.menu_search:
//			
//			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
}
